package com.zlp.netty.component;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;



/**
 * @author: zhulaiping
 * @time: 2022/9/1 13:55
 */
@Slf4j
public class NettyFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();
        Future<Integer> future = eventLoop.submit(() -> {
            log.debug("执行计算。。。");
            Thread.sleep(1000);
            return 50;
        });

        log.debug("等待结果，，，：");
        //阻塞获取
        //log.debug("结果是：{}",future.get());
        log.debug("结果是：{}",future.getNow());
        //使用回调函数，异步获取
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.debug("结果是：{}",future.getNow());
            }
        });
    }
}
